/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import pojo.ThongTinTaiKhoanMail;

/**
 *
 * @author DQ
 */
public class GoiNhanMail {

    public static Message[] NhanMail(final ThongTinTaiKhoanMail mail) {
        Properties props = new Properties();

        try {
            props.put("mail.smtp.host", mail.getPop());
            props.put("mail.smtp.port", mail.getPopport());
            props.put("mail.smtp.starttls.enable", "true");
            javax.mail.Authenticator pa = new javax.mail.Authenticator() {

                public PasswordAuthentication getpassPasswordAuthentication() {
                    return new PasswordAuthentication(mail.getEmail(), mail.getPassword());
                }
            };
            Session ss = Session.getDefaultInstance(props, pa);
            Store store = ss.getStore("pop3s");
            store.connect(mail.getPop(), mail.getEmail(), mail.getPassword());
            Folder inbox = store.getFolder("INBOX");
            Message[] messages = null;
            if (inbox != null) {
                inbox.open(Folder.READ_ONLY);
                messages = inbox.getMessages();
            }
//            ArrayList<Message> arrMess = new ArrayList<>();
//            arrMess.addAll(Arrays.asList(messages));
            //close inbox nhung khong xoa messages[] - nhung chay chua duoc.
//            inbox.close(false);
//            store.close();

            return messages;


        } catch (Exception ex) {
        }



        return null;
    }
}
